/*
 * Copyright (c) 2008 Anshuman Manur <anshuman.manur@gmail.com>,
 * Lavanya Shastri <lavanya.shastri@gmail.com>
 * 
 * This file is part of Andronova.
 * 
 * Andronova is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Andronova is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with Andronova.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This file may use the functionality provided by the library
 * jid3lib version 0.5.4 (<http://sourceforge.net/projects/javamusictag/>).
 * jid3lib and its uses are covered by GNU Lesser General Public License.
 * 
 */

package com.googlecode.andronova.helper;

import java.io.IOException;

public class PlayBackOrganiser {
	
	private Library library;
	private PlayList playList;
	
	private boolean libraryMode = false;
	private boolean playListMode = false;
	private boolean randomMode = false;
	private boolean shuffleMode = false;
	private boolean loopMode = false;
	private boolean repeatMode = false;
	
	private Song currentSong;
		
	private boolean isPlaying = false;
	
	public PlayBackOrganiser(Library library) {
		this.library = library;
		libraryMode = true;
		playListMode = false;
	}
	
	public PlayBackOrganiser(PlayList playList) {
		this.playList = playList;
		playListMode = true;
		libraryMode = false;
	}
	
	public Song getCurrentSong() {
		return currentSong;
	}
	
	public Song getNextSong() throws IOException, EndOfPlayListException {		
		if(repeatMode)
			return currentSong;
		
		if(libraryMode) {
			if(randomMode) {
				double rand = Math.random();
				rand *= 100000;
				int i = (int) rand;
				Song currentSong = library.getSong(i%library.getSize() + 1);
				return currentSong;
			}
			long id = currentSong.getLibId() + 1;
			if(id > library.getSize())
				id = 1;
			currentSong = library.getSong(id);
			return currentSong;
		}
		else {
			if(shuffleMode) {
				long id;
				do {
					double rand = Math.random();
					rand *= 100000;
					int i = (int) rand;
					id = i%playList.getSize() + 1;
				}
				while(!playList.hasId(id));
				
				currentSong = playList.getSong(id);				
				return currentSong;
			}
			
			id = currentSong.getPlayListId();
			do {
				id++;
				if(id > playList.getMaxId())
					if(loopMode)
						id = 1;
					else
						throw new EndOfPlayListException();
			}
			while(!playList.hasId(id));
			
			currentSong = playList.getSong(id);
			return currentSong;
		}
	}
	
	long id;
	
	public Song getPrevSong() throws EndOfPlayListException, IOException {
		if(repeatMode)
			return currentSong;
		
		if(libraryMode) {
			long id = currentSong.getLibId() - 1;
			if(id < 1)
				id = library.getSize();
			currentSong = library.getSong(id);
			return currentSong;
		}
		
		if(randomMode)
			return null;
			
		else {
			if(shuffleMode)
				return null;
			
			long id = currentSong.getPlayListId();
			do {
				id--;
				if(id == 0)
					id = playList.getSize();
			}
			while(!playList.hasId(id));
			
			currentSong = playList.getSong(id);
			return currentSong;
		}		
	}
	
	public void setCurrentSong(Song s) {
		currentSong = s;
	}
	
	public void setLibraryMode(boolean mode) {
		libraryMode = mode;
		playListMode = !mode;		
	}
	
	public void setPlayListMode(boolean mode) {
		playListMode = mode;
		libraryMode = !mode;
	}
	
	public void setRandom(boolean mode) {
		if(libraryMode)
			randomMode = mode;
	}
	
	public void setShuffle(boolean mode) {
		if(playListMode)
			shuffleMode = mode;
	}
	
	public void setLoop(boolean mode) {
		if(playListMode)
			loopMode = mode;
	}
	
	public void setRepeat(boolean mode) {
		repeatMode = mode;
	}
	
	public void setPlaying(boolean b) {
		isPlaying = b;
	}
	
	public boolean isLibraryMode() {
		return libraryMode;
	}
	
	public boolean isPlayListMode() {
		return playListMode;
	}
	
	public boolean isPlaying() {
		return isPlaying;
	}
}
